import java.util.*;

class Lottery3D {
    private String winNum;

    Lottery3D(String winNum) {
        this.winNum = winNum;
    }

    String getWinNum() {
        return winNum;
    }

    int getWins(String userNumber) {
        return 0;
    }

    boolean checkUserNumber(String userNumber) {
        if (userNumber.length() != 3) return false;
        for (int i = 0; i < 3; i++) {
            if (winNum.charAt(i) < '0' || winNum.charAt(i) > '9') return false;
        }
        return true;
    }
}

class Single extends Lottery3D {
    Single(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();
        return userNumber.equals(getWinNum()) ? 1040 : 0;
    }

}

class Group extends Lottery3D {
    Group(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();

        int diffNum = 0;//计算winNum中有几个不同数字
        HashSet<Integer> hash = new HashSet<>();
        for (int i = 0; i < getWinNum().length(); i++) {
            hash.add(getWinNum().charAt(i) - '0');
        }
        diffNum = hash.size();

        char[] winChars = getWinNum().toCharArray();
        char[] userChars = userNumber.toCharArray();
        Arrays.sort(winChars);
        Arrays.sort(userChars);
        if (Arrays.equals(winChars, userChars)) {
            if (diffNum == 2) return 346;
            else if (diffNum == 3) return 173;
        }
        return 0;
    }
}

class Oned extends Lottery3D {
    Oned(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();

        for (int i = 0; i < userNumber.length(); i++) {
            if (userNumber.charAt(i) == getWinNum().charAt(i)) return 10;
        }
        return 0;
    }

    boolean checkUserNumber(String userNumber) {
        int charNum = 0, digitNum = 0;
        for (int i = 0; i < userNumber.length(); i++) {
            if (userNumber.charAt(i) == '*') charNum++;
            else if (userNumber.charAt(i) >= '0' && userNumber.charAt(i) <= '9') digitNum++;
        }
        return charNum == 2 && digitNum == 1;
    }
}

class Guess1d extends Lottery3D {
    Guess1d(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();
        int sameNum = 0;
        for (int i = 0; i < getWinNum().length(); i++) {
            if (userNumber.charAt(0) == getWinNum().charAt(i)) sameNum++;
        }
        if (sameNum == 1) return 2;
        else if (sameNum == 2) return 12;
        else if (sameNum == 3) return 230;
        return 0;
    }

    boolean checkUserNumber(String userNumber) {
        return userNumber.length()==1 &&userNumber.charAt(0) >= '0' && userNumber.charAt(0) <= '9';
    }
}

class TwoD extends Lottery3D {
    TwoD(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();
        int sameNum = 0;
        for (int i = 0; i < getWinNum().length(); i++) {
            if (userNumber.charAt(i) == getWinNum().charAt(i)) sameNum++;
        }
        return sameNum == 2 ? 104 : 0;
    }

    boolean checkUserNumber(String userNumber) {
        int charNum = 0, digitNum = 0;
        for (int i = 0; i < userNumber.length(); i++) {
            if (userNumber.charAt(i) == '*') charNum++;
            else if (userNumber.charAt(i) >= '0' && userNumber.charAt(i) <= '9') digitNum++;
        }
        return charNum == 1 && digitNum == 2;
    }
}

class General extends Lottery3D {
    General(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();
        int sameNum = 0;
        for (int i = 0; i < getWinNum().length(); i++) {
            if (userNumber.charAt(i) == getWinNum().charAt(i)) sameNum++;
        }
        if (sameNum == 3) return 470;
        else if (sameNum == 2) return 21;
        return 0;
    }
}

class Sum extends Lottery3D {
    Sum(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();
        int winSum = 0;
        for (char k : getWinNum().toCharArray()) {
            winSum += k - '0';
        }
        int userSum = Integer.parseInt(userNumber);
        if (winSum == userSum) {
            if (winSum == 0 || winSum == 27) return 1040;
            else if (winSum == 1 || winSum == 26) return 345;
            else if (winSum == 2 || winSum == 25) return 172;
            else if (winSum == 3 || winSum == 24) return 104;
            else if (winSum == 4 || winSum == 23) return 69;
            else if (winSum == 5 || winSum == 22) return 49;
            else if (winSum == 6 || winSum == 21) return 37;
            else if (winSum == 7 || winSum == 20) return 29;
            else if (winSum == 8 || winSum == 19) return 23;
            else if (winSum == 9 || winSum == 18) return 19;
            else if (winSum == 10 || winSum == 17) return 16;
            else if (winSum == 11 || winSum == 16) return 15;
            else if (winSum == 12 || winSum == 15) return 15;
            else if (winSum == 13 || winSum == 14) return 14;
        }
        return 0;
    }

    boolean checkUserNumber(String userNumber) {
        int userSum = Integer.parseInt(userNumber);
        return userSum >=0 && userSum <= 27 && userNumber.length()<=2;
    }
}

class Package extends Lottery3D {
    Package(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if (!checkUserNumber(userNumber)) throw new InputMismatchException();
        int diffNum = 0;//计算winNum中有几个不同数字
        HashSet<Integer> hash = new HashSet<>();
        for (int i = 0; i < getWinNum().length(); i++) {
            hash.add(getWinNum().charAt(i) - '0');
        }
        diffNum = hash.size();
        char[] winChars = getWinNum().toCharArray();
        char[] userChars = userNumber.toCharArray();
        if (Arrays.equals(winChars, userChars)) {
            if (diffNum == 2) return 693;
            else if (diffNum == 3) return 606;
        }
        Arrays.sort(winChars);
        Arrays.sort(userChars);
        if (Arrays.equals(winChars, userChars)) {
            if (diffNum == 2) return 173;
            else if (diffNum == 3) return 86;
        }
        return 0;
    }
}

class Tractor extends Lottery3D {
    Tractor(String winNum) {
        super(winNum);
    }

    int getWins(String userNumber) {
        if ((getWinNum().charAt(0) - getWinNum().charAt(1) == 1 && getWinNum().charAt(1) - getWinNum().charAt(2) == 1) ||
                (getWinNum().charAt(0) - getWinNum().charAt(1) == -1 && getWinNum().charAt(1) - getWinNum().charAt(2) == -1))
            return 65;
        return 0;
    }
}
public class Main {
    static boolean checkWinNum(String winNum) {
        if (winNum.length() != 3) return false;
        for (int i = 0; i < 3; i++) {
            if (winNum.charAt(i) < '0' || winNum.charAt(i) > '9') return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入开奖号码：");
        String winNum = sc.next();
        if (!checkWinNum(winNum)) {
            System.out.println("开奖号码有误，请重试");
            return;
        }
        System.out.println("请输入投注次数：");
        int nTimes = sc.nextInt();
        if (nTimes <= 0) {
            System.out.println("投注次数有误，请重试");
            return;
        }
        int maxWin = 0;
        System.out.println("请输入投注倍数：");
        int betMultiple = sc.nextInt();
        if (betMultiple <= 0) {
            System.out.println("投注倍数有误，请重试");
            return;
        }
        for (int i = 0; i < nTimes; i++) {
            System.out.println("请输入选项：");
            String choice = sc.next();
            try {
                switch (choice) {
                    case "single": {
                        Single single = new Single(winNum);
                        System.out.println("输入投注号码：");
                        int wins = single.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "group": {
                        Group group = new Group(winNum);
                        System.out.println("输入投注号码：");
                        int wins = group.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "oned": {
                        Oned oned = new Oned(winNum);
                        System.out.println("输入投注号码：");
                        int wins = oned.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "guess1d": {
                        Guess1d guess1d = new Guess1d(winNum);
                        System.out.println("输入投注号码：");
                        int wins = guess1d.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "towd": {
                        TwoD twoD = new TwoD(winNum);
                        System.out.println("输入投注号码：");
                        int wins = twoD.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "general": {
                        General general = new General(winNum);
                        System.out.println("输入投注号码：");
                        int wins = general.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "sum": {
                        Sum sum = new Sum(winNum);
                        System.out.println("输入投注号码：");
                        int wins = sum.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "package": {
                        Package pack = new Package(winNum);
                        System.out.println("输入投注号码：");
                        int wins = pack.getWins(sc.next());
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    case "tractor": {
                        Tractor tractor = new Tractor(winNum);
                        int wins = tractor.getWins("");
                        System.out.println("赢了" + wins * betMultiple);
                        maxWin = Math.max(maxWin, wins * betMultiple);
                        break;
                    }
                    default: {
                        System.out.println("选项错误，请重试");
                        i--;// 重试当前投注
                    }
                }
            } catch (InputMismatchException e) {
                System.out.println("输入号码格式错误，请重新输入");
                i--; // 重试当前投注
            } catch (Exception e) {
                System.out.println("发生未知错误，请重新输入");
                i--; // 重试当前投注
            }
        }
        System.out.println("您的最大中奖数为" + maxWin);
    }
}
